package com.example.zhaojianxu.signin.car.mycar

import android.support.v7.widget.RecyclerView
import android.view.View
import com.example.zhaojianxu.signin.car.http.MyCarEntity
import com.example.zhaojianxu.signin.common.http.HttpUtil
import de.greenrobot.event.EventBus
import kotlinx.android.synthetic.main.item_my_car_view.view.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

/**
 * create by @zhaojianxu on 2019/3/1
 */
class MyCarVH(val rootView: View) : RecyclerView.ViewHolder(rootView) {

    fun binData(data: MyCarEntity) {

        rootView.item_car_my_1.text = data.carEntity?.name
        rootView.item_car_my_2.text = data.carEntity?.price.toString()
        rootView.item_car_my_3.text = data.number.toString()

        rootView.item_car_my_4.setOnClickListener {
            deleteCar(data.mid)
        }

    }

    fun deleteCar(mid: Int){
        HttpUtil.instance.getCarApi()?.postDeleteCar(mid)?.enqueue(object : Callback<String>{
            override fun onFailure(call: Call<String>, t: Throwable) {
            }

            override fun onResponse(call: Call<String>, response: Response<String>) {
                if (response.isSuccessful){
                    EventBus.getDefault().post(MyCarEvent())
                }
            }

        })
    }
}